package com.molichuxing.gateway.bff.order.services.impl;

import com.molichuxing.framework.dto.Paged;
import com.molichuxing.framework.dto.TabDto;
import com.molichuxing.framework.utils.ObjectCastUtil;
import com.molichuxing.gateway.bff.order.convertor.Convert;
import com.molichuxing.gateway.bff.order.entity.response.EnterpriseScheduleVo;
import com.molichuxing.gateway.bff.order.entity.response.PaymentEnterpriseInfoVo;
import com.molichuxing.gateway.bff.order.services.OrderEnterpriseScheduleBffService;
import com.molichuxing.services.business.dto.response.EnterpriseScheduleBizDto;
import com.molichuxing.services.business.service.EnterpriseScheduleBizService;
import com.molichuxing.services.infrastructure.dto.response.EnterpriseDto;
import com.molichuxing.services.infrastructure.dto.response.EnterpriseScheduleDto;
import com.molichuxing.services.infrastructure.dto.response.OrderEnterpriseDto;
import com.molichuxing.services.infrastructure.dto.response.PaymentEnterpriseDto;
import com.molichuxing.services.infrastructure.service.EnterpriseScheduleService;
import com.molichuxing.services.infrastructure.service.EnterpriseService;
import com.molichuxing.services.infrastructure.service.OrderEnterpriseService;
import com.molichuxing.services.infrastructure.service.PaymentEnterpriseService;
import com.molichuxing.services.property.EnterpriseScheduleTabTypeEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service("orderEnterpriseScheduleBffService")
public class OrderEnterpriseScheduleBffServiceImpl implements OrderEnterpriseScheduleBffService {
    private static final Logger logger = LoggerFactory.getLogger(OrderEnterpriseScheduleBffServiceImpl.class);

    @Resource
    private EnterpriseScheduleBizService enterpriseScheduleBizService;
    @Resource
    private EnterpriseScheduleService enterpriseScheduleService;
    @Resource
    private PaymentEnterpriseService paymentEnterpriseService;

    @Resource
    private EnterpriseService enterpriseService;

    @Resource
    private OrderEnterpriseService orderEnterpriseService;

    /**
     * 还款管理
     *
     * @param params
     * @param pageNum
     * @param pageSize
     * @return
     * @throws Exception
     */
    @Override
    public Paged<EnterpriseScheduleVo> getEnterpriseSchedulePage(Map<String, Object> params, Integer pageNum, Integer pageSize) throws Exception {
        Integer tabType = ObjectCastUtil.castInteger(params.get("tabType"));
        if (null == tabType) {
            return new Paged<EnterpriseScheduleVo>();
        }

        if (EnterpriseScheduleTabTypeEnum.SCHEDULE.getValue() == tabType
                || EnterpriseScheduleTabTypeEnum.OVERDUE_REPAYMENT.getValue() == tabType) {

            Paged<EnterpriseScheduleBizDto> schedulePage = enterpriseScheduleBizService.getSchedulePage(params, pageNum, pageSize);
            List<EnterpriseScheduleBizDto> bizDtoList = schedulePage.getList();
            if (null == bizDtoList) {
                Paged<EnterpriseScheduleVo> paged = new Paged<EnterpriseScheduleVo>();
                return getPageTab(paged);
            }

            List<Integer> enterpriseIdList = new ArrayList<>();
            for (EnterpriseScheduleBizDto enterpriseScheduleBizDto : bizDtoList) {
                Integer enterpriseId = enterpriseScheduleBizDto.getEnterpriseId();
                enterpriseIdList.add(enterpriseId);
            }

            Map<Integer, EnterpriseDto> enterpriseMap = enterpriseService.getEnterpriseByIds(enterpriseIdList);
            List<EnterpriseScheduleVo> enterpriseScheduleVos = Convert.toEnterpriseScheduleVoList(bizDtoList, enterpriseMap);
            Paged<EnterpriseScheduleVo> paged = new Paged<EnterpriseScheduleVo>(enterpriseScheduleVos, schedulePage.getTotal(), pageSize);
            return getPageTab(paged);
        } else if (EnterpriseScheduleTabTypeEnum.PAYMENT_RECORD.getValue() == tabType) {
            Paged<PaymentEnterpriseDto> enterprisePage = paymentEnterpriseService.getEnterprisePage(params, pageNum, pageSize);
            List<PaymentEnterpriseDto> bizDtoList = enterprisePage.getList();
            if (null == bizDtoList) {
                Paged<EnterpriseScheduleVo> paged = new Paged<EnterpriseScheduleVo>();
                return getPageTab(paged);
            }

            List<Integer> scheduleIdList = new ArrayList<>();
            List<Integer> enterpriseIdList = new ArrayList<>();
            List<Long> orderCodes = new ArrayList<>();

            for (PaymentEnterpriseDto enterpriseDto : bizDtoList) {
                Integer scheduleId = enterpriseDto.getScheduleId();
                Integer enterpriseId = enterpriseDto.getEnterpriseId();
                Long orderCode = enterpriseDto.getOrderCode();
                Integer repaymentPeriods = enterpriseDto.getRepaymentPeriods();

                scheduleIdList.add(scheduleId);
                enterpriseIdList.add(enterpriseId);
                if (repaymentPeriods != null && repaymentPeriods == 1) {
                    orderCodes.add(orderCode);
                }
            }

            Map<Integer, EnterpriseScheduleBizDto> scheduleMap = enterpriseScheduleBizService.getScheduleByScheduleIds(scheduleIdList);
            Map<Integer, EnterpriseDto> enterpriseMap = enterpriseService.getEnterpriseByIds(enterpriseIdList);
            Map<Long, OrderEnterpriseDto> orderEnterpriseMap = orderEnterpriseService.getEnterpriseMap(orderCodes);
            List<EnterpriseScheduleVo> enterpriseScheduleVos = Convert.toEnterpriseScheduleList(bizDtoList, scheduleMap, enterpriseMap,orderEnterpriseMap);
            Paged<EnterpriseScheduleVo> paged = new Paged<EnterpriseScheduleVo>(enterpriseScheduleVos, enterprisePage.getTotal(), pageSize);
            return getPageTab(paged);
        } else {
            return new Paged<EnterpriseScheduleVo>();
        }
    }

    /**
     * 详情
     *
     * @param scheduleId
     * @return
     */
    @Override
    public PaymentEnterpriseInfoVo getPaymentEnterpriseInfo(Integer scheduleId) {
        EnterpriseScheduleDto enterpriseScheduleDto = enterpriseScheduleService.getScheduleById(scheduleId);
        PaymentEnterpriseDto paymentEnterpriseDto = paymentEnterpriseService.getByScheduleId(scheduleId);
        return Convert.toPaymentEnterpriseInfoVo(enterpriseScheduleDto, paymentEnterpriseDto);
    }

    /**
     * 获取分页tab值
     *
     * @return
     * @throws Exception
     */
    private Paged<EnterpriseScheduleVo> getPageTab(Paged<EnterpriseScheduleVo> paged) throws Exception {
        Integer scheduleCount = enterpriseScheduleService.getScheduleCount();
        Integer scheduleOverdueCount = enterpriseScheduleService.getScheduleOverdueCount();
        Integer enterpriseCount = paymentEnterpriseService.getEnterpriseCount();

        List<TabDto> tabList = new ArrayList<>();
        for (EnterpriseScheduleTabTypeEnum typeEnum : EnterpriseScheduleTabTypeEnum.values()) {
            int value = typeEnum.getValue();
            String name = typeEnum.getName();
            int count = 0;
            if (EnterpriseScheduleTabTypeEnum.SCHEDULE.getValue() == value) {
                count = scheduleCount;
            }
            if (EnterpriseScheduleTabTypeEnum.OVERDUE_REPAYMENT.getValue() == value) {
                count = scheduleOverdueCount;
            }
            if (EnterpriseScheduleTabTypeEnum.PAYMENT_RECORD.getValue() == value) {
                count = enterpriseCount;
            }

            TabDto tabDto = new TabDto();
            tabDto.setName(name);
            tabDto.setType(value);
            tabDto.setCount(count);
            tabList.add(tabDto);
        }

        return paged.setTab(tabList);
    }

}
